package Twitim::Assistant;

use strict;
use warnings;
use utf8;

use Gtk2;
use Glib qw(TRUE FALSE);
use Twitim::L10N;
use Twitim::Twitim;
use Twitim::API::OAuth;

my ( %Entry, %Label );
my ( $Token, $PIN, %Access_Token );

binmode( STDOUT, ':utf8' );

sub new {
    my $this  = shift;
    my $class = ref($this) || $this;
    my $self  = { quit_when_cancel => TRUE, @_ };
    bless $self, $class;
}

sub start {
    my ( $self, $parent ) = @_;

    my $assistant = Gtk2::Assistant->new();
    $assistant->set_default_size( 550, 300 );
    $assistant->set_position('center');
    $assistant->set_title( l10n("Twitim Account Setup Assistant") );
    $assistant->set_icon_from_file($main::APP_ICON) if $main::APP_ICON;
    $assistant->set_transient_for($parent) if $parent;

    $assistant->signal_connect( cancel  => \&_cancel_cb, $self );
    $assistant->signal_connect( close   => \&_close_cb );
    $assistant->signal_connect( prepare => \&_prepare_cb );
    $assistant->signal_connect( apply   => $self->{'on_destroy'} );

    my ( $text, $page );

    $text = $self->{'quit_when_cancel'} ? l10n("Welcom to Twitim.  ") : "";
    $text
        .= l10n(
        "This assistant helps you to configure accounts for Twitim."
        ) . "\n\n";
    $text
        .= l10n(
        "Click \"Forward\" to open a Twitter authorization page with the default browser."
        );

    $page = _make_simple_label( $text, 0.5 );
    $assistant->append_page($page);

    my $page1_title = $self->{'quit_when_cancel'} ? "Welcome" : "Twitter Account";
    $assistant->set_page_title( $page, l10n($page1_title) );

    $assistant->set_page_type( $page, 'intro' );
    $assistant->set_page_complete( $page, TRUE );

    $text
        = "Return to this dialog after you have finished the authorization process on Twitter.com and Enter your PIN below.";

    $page = _entry_vbox( l10n($text), "twitter", $assistant );
    $assistant->append_page($page);
    $assistant->set_page_title( $page, l10n("Enter PIN") );
    $assistant->set_page_type( $page, 'content' );
    $assistant->set_page_complete( $page, FALSE );

    $text
        = "Your configuration is complete.  You are now ready to access to Twitter with Twitim.\n\nClick \"Apply\" to save your settings.";
    $page = _confirm_vbox( l10n($text) );
    $assistant->append_page($page);
    $assistant->set_page_title( $page, l10n("Done") );
    $assistant->set_page_type( $page, 'confirm' );
    $assistant->set_page_complete( $page, FALSE );

    $assistant->show_all;
}

sub get_data {
    my $self = shift;
    return %Access_Token;
}

###########################################################################
# callback

sub _prepare_cb {
    my ( $assistant, $page ) = @_;
    my $page_num = $assistant->get_current_page();
    my $auth     = Twitim::API::OAuth->new();

    if ( $page_num == 1 ) {
        ( my $url, $Token ) = $auth->open_authorize_url();
        open_url($url);
    }
    elsif ( $page_num == 2 ) {
        my $pin = $Entry{'PIN'}->get_text;

        %Access_Token = $auth->get_access_token( $Token, $pin );
        $Label{'twitter'}->set_text( $Access_Token{'user'} );

        $assistant->set_page_complete( $page, TRUE );
    }
}

sub _close_cb {
    my $widget = shift;
    # print "close\n";
    $widget->hide;
}

sub _cancel_cb {
    my ( $widget, $self ) = @_;
    # print "cancel\n";
    $widget->destroy;
    Gtk2->main_quit if $self->{'quit_when_cancel'};
}

sub _entry_changed_cb {
    my ( $widget, $data ) = @_;

    my $name     = $data->{'name'};
    my $page_num = $data->{'assistant'}->get_current_page;
    my $page     = $data->{'assistant'}->get_nth_page($page_num);

    my $pin = $Entry{'PIN'}->get_text;
    my $state = ( $pin =~ /^[0-9]+$/ ) ? TRUE : FALSE;

    $data->{'assistant'}->set_page_complete( $page, $state );
}

######################################################################
# GUI parts

sub _make_simple_label {
    my ( $text, $x_align ) = @_;

    my $label = Gtk2::Label->new;
    $label->set_markup($text);
    $label->set_line_wrap(TRUE);
    $label->set_line_wrap_mode('word-char');

    # $label->set_width_chars(55);
    $label->set_alignment( $x_align, 0.5 );

    return $label;
}

sub _entry_vbox {
    my ( $label, $acc, $top ) = @_;
    my ( $vbox, $table ) = _make_vbox($label);

    my ( $type, %data );

    $type = "PIN";
    $Entry{$type} = text_entry( $table, l10n("_PIN"), undef );
    %data = ( assistant => $top, page => $type, );
    $Entry{$type}->signal_connect( changed => \&_entry_changed_cb, \%data );

    return $vbox;
}

sub _confirm_vbox {
    my $label = shift;
    my ( $vbox, $table ) = _make_vbox($label);

    set_label( $table, l10n("Twitter User Name"), undef, undef, undef, TRUE );
    $Label{'twitter'} = set_label( $table, "", 0, 1 );
    $Label{'twitter'}->set_text( l10n('Unsetting') );

    return $vbox;
}

sub _make_vbox {
    my $name = shift;

    my $vbox = Gtk2::VBox->new( 0, 2 );
    $vbox->set_border_width(12);

    my $label = _make_simple_label( $name, 0 );
    $vbox->pack_start( $label, 0, 0, 0 );

    my $table = Gtk2::Table->new( 2, 2 );
    $table->set_col_spacings(12);
    $table->set_row_spacings(6);
    $table->set_border_width(6);

    my $alignment = Gtk2::Alignment->new( 0.5, 0.5, 1.0, 1.0 );
    $alignment->set_padding( 12, 0, 12, 0 );
    $alignment->add($table);
    $vbox->pack_start( $alignment, 0, 0, 0 );

    return ( $vbox, $table );
}

1;
